case when语句(SQL)多种用法 |
您所在的位置:网站首页 › gbase case when 语法 › case when语句(SQL)多种用法 |
case具有两种格式。简单case函数和case搜索函数。 --简单case函数 case sex when '1' then '男' when '2' then '女’ else '其他' end --case搜索函数 case when sex = '1' then '男' when sex = '2' then '女' else '其他' end 这两种方式可以实现相同的功能,简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式。 where 1=1 and (case when s.PROVID = '0000' then s.PROVID when s.PROVID '0000' then t.areacod else t.areacod end) = (case when s.PROVID = '0000' then '0000' when s.PROVID '0000' then (select max(t1.areacod) from posctycod t1 where t1.provid=s.provid and t1.cityid = s.cityid) else '' end)
还有一个需要注重的问题,case函数只返回第一个符合条件的值,剩下的case部分将会被自动忽略。 --比如说,下面这段sql,你永远无法得到“第二类”这个结果 case when col_1 in ('a','b') then '第一类' when col_1 in ('a') then '第二类' else '其他' end
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |